// Copyright (c) 2022 NetEase, Inc. All rights reserved.
// Use of this source code is governed by a MIT license that can be
// found in the LICENSE file.

import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:hawk/hawk.dart';
import 'package:nim_core/nim_core.dart';
import 'package:http/http.dart' as http;
import 'package:path_provider/path_provider.dart';
import '../main_test.dart';

Future<String> networkUrlToBase64(String url) async {
  http.Response response = await http.get(Uri.parse(url));
  final bytes = response.bodyBytes;
  final mimeType = response.headers['content-type']?.split(';')[0] ??
      'application/octet-stream';
  return Uri.dataFromBytes(bytes, mimeType: mimeType).toString();
}

class HandleNosCase extends HandleBaseCase {
  HandleNosCase();

  @override
  Future<ResultBean?> handle(event) async {
    super.handle(event);
    var result;
    if (className?.trim() != 'NOSService') {
      return null;
    }
    switch (methodName?.trim()) {
      case 'upload':
        {
          var map = params![0];
          String filePath = map['filePath'];
          if (kIsWeb) {
            filePath = await networkUrlToBase64(map['filePath']);
          }
          if (Platform.isIOS || Platform.isMacOS) {
            Directory rootPath = await getApplicationDocumentsDirectory();
            filePath = '${rootPath.path}/$filePath';
          }
          final ret = await NimCore.instance.nosService.upload(
              filePath: filePath,
              mimeType: map['mimeType'],
              sceneKey: map['sceneKey']);
          result = ret;
          handleCaseResult = ResultBean(
            code: result.code,
            data: result.data,
            message: result.errorDetails,
          );
          break;
        }
      case 'download':
        {
          var map = params![0];
          Directory rootPath = await getApplicationDocumentsDirectory();
          var filePath = '${rootPath.path}/${map['path']}';
          final ret = await NimCore.instance.nosService
              .download(url: map['url'], path: filePath);
          result = ret;
          handleCaseResult = ResultBean(
            code: result.code,
            data: result.toMap(),
            message: result.errorDetails,
          );
          break;
        }
      case 'onNOSTransferStatus':
        {
          subsriptions.add(
              NimCore.instance.nosService.onNOSTransferStatus.listen((event) {
            print('NOSService#onNOSTransferStatus ${event.toMap()}');
            IntegratedManager.instance.report('onNOSTransferStatus',
                ResultBean(code: 0, data: event.toMap()));
          }));
          handleCaseResult = ResultBean.success(data: null);
          break;
        }
      case 'onNOSTransferProgress':
        {
          subsriptions.add(
              NimCore.instance.nosService.onNOSTransferProgress.listen((event) {
            print('NOSService#onNOSTransferProgress $event');
            IntegratedManager.instance.report(
                'onNOSTransferProgress', ResultBean(code: 0, data: event));
          }));
          handleCaseResult = ResultBean.success(data: null);
          break;
        }
    }
    return handleCaseResult;
  }
}
